package com.safebus.manager;



import com.safebus.R;

import android.content.Context;
import android.os.AsyncTask;

public class ApiManager extends AsyncTask<Void, Integer, String> {
	DataDownloadListener dataDownloadListener;
	InternetManager manager;
	String response, url;
	String[] name, value;
	Context context;
	private boolean isProgress;
	private CustomProgressDialog progress;
	
	private boolean isException=false;
	private boolean isInternetException=false;
	private String errMsg="";
	public ApiManager(String url, String[] name, String[] value
			,boolean isProgress,Context context) {
		this.url = url;
		this.name = name;
		this.value = value;
		this.isProgress=isProgress;
		 this.context=context;
		 
		 
		 if(isProgress)
		 progress = CustomProgressDialog.show(context, false);
	}

	public void setDataDownloadListener(
			DataDownloadListener dataDownloadListener) {
		this.dataDownloadListener = dataDownloadListener;
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		
		if(isProgress)
		progress.show();
		
		isException=false;
		isInternetException=false;
		errMsg="";
	}

	@Override
	protected String doInBackground(Void... params) {
		
		if(Utils.checkInternetConnection(context)){

		manager = new InternetManager();
		try {
			response = manager.commonPostData(url, name, value);
			System.out.println("Api Class Response" + response);
		} catch (Exception e) {
			isException=true;
			e.printStackTrace();
			errMsg=e.getMessage().toString();
		}
		
		}
		else{
			
			isException=true;
			isInternetException=true;
		}
		
		return response;
	}

	@Override
	protected void onPostExecute(String result) {
		super.onPostExecute(result);
		if(isProgress)
		progress.dismiss();
		
		if(isException)
		{
			
			if(isInternetException){
				NotifyManager.showShortToast(context, context.getString(R.string.Internet_check));
			}
			else{
				
				NotifyManager.showShortToast(context, context.getString(R.string.common_error_message));
				dataDownloadListener.dataDownloadedError(errMsg);	
			}
			
		}
		else{
			
			Utils.longInfo(result);
			dataDownloadListener.dataDownloadedSuccessfully(result);
		}
		
		
	}

	public static interface DataDownloadListener {
		void dataDownloadedSuccessfully(String response);
		void dataDownloadedError(String err);

	}

}
